package StoreServer;

import java.rmi.RemoteException;
import java.util.Iterator;
import java.util.TreeSet;
import java.util.Vector;

import be.ac.ua.codebase.Product;
import be.ac.ua.codebase.StoreInterface;

public class StoreImplementation implements StoreInterface {
	Vector<Product> fProducts = new Vector<Product>();

	public StoreImplementation() throws Exception {
		
	}
	
	public int getLoad() throws RemoteException {
		return fProducts.size();
	}
	
	public void addProduct(Product product) throws RemoteException {
		System.out.println("[STORE] Add product with name " + product.getName());
		fProducts.add(product);
	}
	
	public Vector<Product> search(String string) throws RemoteException {
		System.out.println("[STORE] Search: " + string);
		Vector <Product> matches = new Vector<Product>();
		for (int i = 0; i < fProducts.size(); i++) {
			if (fProducts.get(i).getName().contentEquals(string)) {
				matches.add(0, fProducts.get(i));
			} else if ((fProducts.get(i).getName().contains(string)) || (string.contains(fProducts.get(i).getName()))) {
				matches.add(fProducts.get(i));
			}
		}
		return matches;
	}
}
